<template>
  <div>

    <div style="margin-bottom:20px">
        <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>商店管理</el-breadcrumb-item>
        <el-breadcrumb-item>商店列表</el-breadcrumb-item>
        </el-breadcrumb>
    </div>

  <el-col span="4">
    <el-input
      placeholder="请输入内容"
      v-model.trim="searchMsg"
      v-on:input="fetchShop">
      <i slot="prefix" class="el-input__icon el-icon-search" id="search_input"></i>
    </el-input>
  </el-col>

  <el-table
    stripe
    @row-click="jump_to_goods"
    v-loading="loading"
    ref="multipleTable"
    :data="shops"
    tooltip-effect="dark"
    style="width: 100%"
    height="550"
    @selection-change="handleSelectionChange"
    >
    <el-table-column
      type="selection"
      width="55">
    </el-table-column>
    <el-table-column
      prop="shop_id"
      label="id"
      sortable
      width="120">
      <!-- <template slot-scope="scope">{{ scope.row.id }}</template> -->
    </el-table-column>
    <el-table-column
      prop="shop_name"
      label="商店名"
      sortable
      width="120">
    </el-table-column>
    <el-table-column
      label="图片"
      show-overflow-tooltip>
      <template slot-scope="scope">
          <el-image :src="scope.row.shop_src" style="height:120px"/>
      </template>
    </el-table-column>
    
    <el-table-column
        fixed="right"
        label="操作"
        width="150">
        <template slot-scope="scope">
            <el-button @click.native.stop="deleteShop(scope.row)" type="danger" size="mini" >删除</el-button>
            <el-button size="mini" @click.native.stop="editButtonClick(scope.row)">编辑</el-button>
        </template>
    </el-table-column>
  </el-table>
<el-row :gutter="10" style="margin-top:20px">
    <el-col :span="2">
          <el-button type="danger" size="small" :disabled="multipleSelection.length==0? true:false" @click="deleteSelect()">删除所选</el-button>
    </el-col>
    <el-col :span="2">
        <shopCreated></shopCreated>
    </el-col>
    <el-col :span="2">
        <el-button type="success" size="small" @click="getShopList()">刷新</el-button>
    </el-col>
</el-row>
<el-row>
  <div class="block">
    <el-pagination
      @current-change="handleCurrentChange"
      :current-page="currentPage"
      :page-size="10"
      layout="total, prev, pager, next"
      :total="total">
    </el-pagination>
  </div>
</el-row>

   <el-dialog title="修改商店" :visible.sync="dialogFormVisible">
        <el-form :model="selectedShop">
          <el-form-item label="商店名" :label-width="formLabelWidth">
            <el-input v-model="selectedShop.shop_name" autocomplete="off"></el-input>
          </el-form-item>
          <el-form-item label="图片地址" :label-width="formLabelWidth">
            <el-input v-model="selectedShop.shop_src" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editShop()">确 定</el-button>
        </div>
      </el-dialog>

</div>
</template>



<script>
import shopCreated from './shopCreated'

// 节流函数
const delay = (function() {
  let timer = 0;
  return function(callback, ms) {
    clearTimeout(timer);
    timer = setTimeout(callback, ms);
  };
})();

export default {
    
    name:"userList",
    components: {shopCreated},
    data(){
        return {
            loading:true,
            formLabelWidth:300,
            dialogFormVisible: false,
            multipleSelection: [],
            shops:[],
            selectedShop:{},
            form:{},
            searchMsg:'',
            currentPage:1,
            total:0,
            types:[],
        }
    },

    created(){
      this.getType()
        this.getShopList()
    },

    methods:{
      getType(){
        this.$axios.get('getAllTypes').then(resp=>{
          if(resp.status==200){
            this.types = resp.data.data
            console.log(this.types)
          }
          
        })
      },
        getShopList(){
            this.loading = true
            this.$axios.get('/listShopsBySearchMsg',{
                params:{
                    currentPage:this.currentPage,
                    searchMsg:this.searchMsg,
                }
            }).then(resp =>{
                if(resp && resp.status === 200){
                    console.log(resp.data)
                    this.shops = resp.data.data
                    this.total = this.shops.length
                    this.loading=false
                }
            })
        },
        
        fetchShop(){
          this.currentPage = 1
          clearTimeout(this.timer);
          this.timer=setTimeout(()=>{
            this.getShopList()
          },800);
        },

        
        handleCurrentChange(val) {
            if(this.currentPage!=val){
              this.currentPage = val;
              this.getShopList()
            }
        },

        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },

        deleteShop(shop){
          this.$axios.get('/deleteShop?id='+shop.shop_id).then(resp=>{
            if(resp.status==200){
              this.$message({message: '删除成功', type: 'success'});
              this.getShopList()
            }
          })
        },
        editButtonClick(shop){
            //解决浅拷贝
            var _shop = JSON.stringify(shop)
            this.selectedShop = JSON.parse(_shop)
            this.dialogFormVisible = true
        },
        editShop(){
          this.$axios.post('/editShop',this.selectedShop).then(resp=>{
            if(resp.status==200){
                this.$message({message:"修改成功",type:'success'})
                this.dialogFormVisible=false
                this.getShopList()
            }
          })
        },
        handleSelectionChange(val) {
            this.multipleSelection = val;
            console.log(this.multipleSelection);
        },
        deleteSelect(){
          for(let item of this.multipleSelection){
            this.deleteShop(item)
          }
        },
        jump_to_goods(row){

            this.$router.push({path:'/index/goodslist',query:{id:row.shop_id}})
        },
        handleCurrentChange(val) {
            if(this.currentPage!=val){
              this.currentPage = val;
              this.getShopList()
            }
        },
    }
}
</script>

<style>

</style>